home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / Movie3.0 / Source / mpegDecode / 24bit.c next >
Encoding:
C/C++ Source or Header  |  1993-03-05  |  4.5 KB  |  189 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include "video.h"
  22. #include "dither.h"
  23. #include "proto.h"
  24.  
  25. /*
  26.  * We'll define the "ConvertColor" macro here to do fixed point arithmetic
  27.  * that'll convert from YCrCb to RGB using:
  28.  *    R = L + 1.40200*Cr;
  29.  *    G = L - 0.34414*Cb - 0.71414*Cr
  30.  *    B = L + 1.77200*Cb;
  31.  *
  32.  * We'll use fixed point by adding two extra bits after the decimal.
  33.  */
  34.  
  35. #define BITS    8
  36. #define ONE     ((int) 1)
  37. #define CONST_SCALE    (ONE << BITS)
  38. #define ROUND_FACTOR    (ONE << (BITS-1))
  39.  
  40. /* Macro to convert integer to fixed. */
  41. #define UP(x)    (((int)(x)) << BITS)
  42.  
  43. /* Macro to convert fixed to integer (with rounding). */
  44. #define DOWN(x)    (((x) + ROUND_FACTOR) >> BITS)
  45.  
  46. /* Macro to convert a float to a fixed */
  47. #define FIX(x)  ((int) ((x)*CONST_SCALE + 0.5))
  48.  
  49. #define CLAMP(ll,x,ul)    ( ((x)<(ll)) ?(ll):( ((x)>(ul)) ?(ul):(x)))
  50.  
  51. static int *Cb_r_tab, *Cr_g_tab, *Cb_g_tab, *Cr_b_tab;
  52.  
  53. /*
  54.  *--------------------------------------------------------------
  55.  *
  56.  * InitColorDither --
  57.  *
  58.  *    To get rid of the multiply and other conversions in color
  59.  *    dither, we use a lookup table.
  60.  *
  61.  * Results:
  62.  *    None.
  63.  *
  64.  * Side effects:
  65.  *    The lookup tables are initialized.
  66.  *
  67.  *--------------------------------------------------------------
  68.  */
  69.  
  70. void
  71. InitColorDither()
  72. {
  73.     int CR, CB, i;
  74.  
  75.     Cr_b_tab = (int *)malloc(256*sizeof(int));
  76.     Cr_g_tab = (int *)malloc(256*sizeof(int));
  77.     Cb_g_tab = (int *)malloc(256*sizeof(int));
  78.     Cb_r_tab = (int *)malloc(256*sizeof(int));
  79.  
  80.     for (i = 0; i < 256; i++)
  81.     {
  82.         CB = CR = i - 128;
  83.     
  84.         Cb_r_tab[i] = FIX(1.40200) * CB;
  85.         Cr_g_tab[i] = -FIX(0.34414) * CR;
  86.         Cb_g_tab[i] = -FIX(0.71414) * CB;   
  87.         Cr_b_tab[i] = FIX(1.77200) * CR;
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *
  95.  * ColorDitherImage --
  96.  *
  97.  *    Converts image into 24 bit color.
  98.  *
  99.  * Results:
  100.  *    None.
  101.  *
  102.  * Side effects:
  103.  *    None.
  104.  *
  105.  *--------------------------------------------------------------
  106.  */
  107.  
  108. void ColorDitherImage(lum, cr, cb, out, rows, cols)
  109.   unsigned char *lum;
  110.   unsigned char *cr;
  111.   unsigned char *cb;
  112.   unsigned char *out;
  113.   int cols, rows;
  114.  
  115. {
  116.     int L, CR, CB;
  117.     unsigned int rowsize;
  118.     unsigned char *out2;
  119.     unsigned char *lum2;
  120.     int x, y;
  121.     unsigned int r, b, g;
  122.     int cb_r;
  123.     int cr_g;
  124.     int cb_g;
  125.     int cr_b;
  126.  
  127.     rowsize = cols * 3;
  128.     out2 = out + rowsize;
  129.     lum2 = lum + cols;
  130.     for (y=0; y<rows; y+=2) {
  131.     for (x=0; x<cols; x+=2) {
  132.         int R, G, B;
  133.  
  134.         CR = *cr++;
  135.         CB = *cb++;
  136.         cb_r = Cb_r_tab[CB];
  137.         cr_g = Cr_g_tab[CR];
  138.         cb_g = Cb_g_tab[CB];
  139.         cr_b = Cr_b_tab[CR];
  140.  
  141.         L = *lum++;
  142.         L = UP(L);
  143.         R = L + cb_r;
  144.         G = L + cr_g + cb_g;
  145.         B = L + cr_b;
  146.         *out++ = CLAMP(0,R,UP(255)) >> BITS;
  147.         *out++ = CLAMP(0,G,UP(255)) >> BITS;
  148.         *out++ = CLAMP(0,B,UP(255)) >> BITS;
  149.  
  150.         L = *lum++;
  151.         L = UP(L);
  152.         R = L + cb_r;
  153.         G = L + cr_g + cb_g;
  154.         B = L + cr_b;
  155.         *out++ = CLAMP(0,R,UP(255)) >> BITS;
  156.         *out++ = CLAMP(0,G,UP(255)) >> BITS;
  157.         *out++ = CLAMP(0,B,UP(255)) >> BITS;
  158.  
  159.         /*
  160.          * Now, do second row.
  161.          */
  162.         L = *lum2++;
  163.         L = UP(L);
  164.         R = L + cb_r;
  165.         G = L + cr_g + cb_g;
  166.         B = L + cr_b;
  167.         *out2++ = CLAMP(0,R,UP(255)) >> BITS;
  168.         *out2++ = CLAMP(0,G,UP(255)) >> BITS;
  169.         *out2++ = CLAMP(0,B,UP(255)) >> BITS;
  170.  
  171.         L = *lum2++;
  172.         L = UP(L);
  173.         R = L + cb_r;
  174.         G = L + cr_g + cb_g;
  175.         B = L + cr_b;
  176.         *out2++ = CLAMP(0,R,UP(255)) >> BITS;
  177.         *out2++ = CLAMP(0,G,UP(255)) >> BITS;
  178.         *out2++ = CLAMP(0,B,UP(255)) >> BITS;
  179.     }
  180.     lum += cols;
  181.     lum2 += cols;
  182.     out += rowsize;
  183.     out2 += rowsize;
  184.     }
  185. }
  186.  
  187.  
  188.  
  189.